package stack_queue;

import java.util.Stack;

public class Test {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i = 0;i<s.length();i++){
            char ch = s.charAt(i);
            if(ch == '(' || ch == '{' || ch == '[' ){
                stack.push(ch);
            }else {
                if(stack.empty()){
                    return false;
                }
                if(ch == ')') {
                    if (stack.peek() == '(') {
                        stack.pop();
                    } else {
                        return false;
                    }
                }
                    if(ch == ']') {
                        if (stack.peek() == '[') {
                            stack.pop();
                        } else {
                            return false;
                        }
                    }if(ch == '}') {
                        if (stack.peek() == '{') {
                            stack.pop();
                        } else {
                            return false;
                        }
                    }
                }
            }
        return stack.empty();
        }











      public int evalRPN(String[] tokens) {
         Stack<Integer> stack = new Stack<>();
         for(String str : tokens){
             if(!isOp(str)){
                 stack.push(Integer.parseInt(str));
             }else {
                 int right = stack.pop();int left = stack.pop();
                 switch (str){
                     case "+":
                         stack.push(left+right);
                         break;
                     case "-":
                         stack.push(left-right);
                         break;
                     case "*":
                         stack.push(left*right);
                         break;
                     case "/":
                         stack.push(left/right);
                         break;
                 }
             }

         }
        return stack.peek();
      }
      private boolean isOp(String s){
        if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/")){
            return true;
        }
        return false;
      }
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        //123456  45321
        // write code here
        int i = 0;int j = 0;
        Stack<Integer> stack = new Stack<>();
        while (i<pushV.length&&j<popV.length){
            stack.push(pushV[i]);
            i++;
           while(stack.peek()==popV[j]){
               stack.pop();
               j++;
           }
        }
        if (j!=popV.length){
            return false;
        }
        return stack.empty();
    }




































}

